<template>
  <!-- header -->
  <div class="header">
    <van-nav-bar title="时间里的" />
    <!-- 搜索框 -->
    <van-search
      v-model="search"
      show-action
      placeholder="请输入搜索关键词(本站采用sphinx搜索引擎)"
    >
      <template #action>
        <div @click="getSearchData">搜索</div>
      </template>
    </van-search>
    <!-- <van-search v-model="search" placeholder="请输入搜索关键词(本站采用sphinx搜索引擎)" clearable @click-left-icon="getSearchData" /> -->
  </div>
  <!-- body -->
  <!-- <van-pull-refresh v-model="loading" @refresh="onRefresh"> -->
  <div class="body">
    <van-tabs v-model:active="active" animated @click-tab="onClickTab">
      <!-- 全部 -->
      <van-tab title="全部" name="0">
        <div
          class="article"
          v-for="(item, index) in dataList"
          :key="index"
          @click="jumpArticleDetail(item.id)"
        >
          <div class="title" v-html="item.arttitle"></div>
          <div class="desc">
            <div class="left-div">
              <div class="author">{{ item.another }} | {{ item.putime }}</div>
              <div class="desc-show" v-html="item.artdesc"></div>
            </div>
            <div class="right-div">
              <img :src="item.artlogo" style="width: 100%" class="lazyload" />
            </div>
          </div>
          <div class="label">
            <div class="label-left">
              <van-icon name="browsing-history-o" />&nbsp;{{ item.click_num }}
            </div>
            <div class="label-right">
              {{ item.labelStr }}
            </div>
          </div>
        </div>
        <div class="next-page" @click="nextPage" v-if="articlePage > page">
          点击加载下一页
        </div>
      </van-tab>
      <!-- 循环 -->
      <van-tab
        v-for="(it, ind) in categoryList"
        :key="ind"
        :title="it.cat_name"
        :name="it.id"
      >
        <div
          class="article"
          v-for="(item, index) in dataList"
          :key="index"
          @click="jumpArticleDetail(item.id)"
        >
          <div class="title" v-html="item.arttitle"></div>
          <div class="desc">
            <div class="left-div">
              <div class="author">{{ item.another }} | {{ item.putime }}</div>
              <div class="desc-show" v-html="item.artdesc"></div>
            </div>
            <div class="right-div">
              <img :src="item.artlogo" style="width: 100%" class="lazyload" />
            </div>
          </div>
          <div class="label">
            <div class="label-left">
              <van-icon name="browsing-history-o" />&nbsp;{{ item.click_num }}
            </div>
            <div class="label-right">
              {{ item.labelStr }}
            </div>
          </div>
        </div>
        <div class="next-page" @click="nextPage" v-if="articlePage > page">
          点击加载下一页
        </div>
      </van-tab>
    </van-tabs>
  </div>
  <img
    src="https://resource.guanchao.site/uploads/gotop/timg.png"
    class="go_top"
    @click="goToTop"
  />
  <!-- </van-pull-refresh> -->
  <!-- navbar -->
  <div class="footer">
    <!-- <Menu @getIndexDataList="goIndex" ref="MenuRef" /> -->
    <Menu @getIndexDataList="goIndex" ref="MenuRef" />
  </div>
</template>

<script lang="ts" setup>
import {
  PropType,
  ref,
  watch,
  reactive,
  toRefs,
  provide,
  inject,
  onBeforeMount, // 在组件挂载之前执行的函数
  onMounted,
  nextTick,
} from "vue";
// 引入路由
import { useRouter, useRoute } from "vue-router";
// 引入公共js文件
import utils from "/@/assets/js/public/function";
// 引入子组件
import Menu from "/@/components/phone/Menu.vue";
import { common, userinfo } from "/@/hooks/common";
import { Toast } from "vant";

// =============================================================================
// 实例化路由
const router = useRouter();
const route = useRoute();

/**
 * @name: 声明data
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-01-18
 */
const data = reactive({
  // 文章列表
  dataList: Array<any>(),
  // 分类列表
  categoryList: Array<any>(),
  // 页码
  page: <number>(route.query.page ? route.query.page : 1),
  // 子分类id
  category_id: <number>(route.query.category_id ? route.query.category_id : 0),
  // 页码（控制加载下一页是否显示）
  articlePage: <number>1,
  // 是否请求
  req: <boolean>(route.query.req ? route.query.req : true),
  // 下拉刷新标识
  loading: <boolean>false,
  // 搜索值
  search: <string>"",
});

/**
 * @name: 将data绑定值dataRef
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-01-10
 */
const {
  dataList,
  categoryList,
  page,
  category_id,
  articlePage,
  loading,
  search,
} = toRefs(data);

// ===============================================================================
// 子组件相关
/**
 * @name: 定义子组件暴露给父组件的值属性列表
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
interface menuData {
  goIndex: () => void;
  getIndexDataList: () => void;
  homeColor: string;
  otherColor: string;
  placeFileColor: string;
  personalColor: string;
}
// 子组件ref（TypeScript语法）下面这这两种写法也可以，推荐使用Typescript语法
const MenuRef = ref<InstanceType<typeof Menu> & menuData>();
// const MenuRef = ref<InstanceType<typeof Menu>>()
// const MenuRef = ref(null)

// ===============================================================================
// 方法
const goToTop = () => {
  // 滚动条回顶部
  let json = document.getElementById("body");
  if (json != null) {
    json.scrollTop = 0;
  }
};

/**
 * @name: 下拉刷新方法
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
const onRefresh = () => {
  // 执行子组件方法
  MenuRef.value?.getIndexDataList(data.page);
  Toast("刷新成功");
  data.loading = false;
};

/**
 * @name: 跳转文章详情页
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 * @param:	category_id	number	分类
 */
const jumpArticleDetail = (article_id: Number) => {
  router.push({
    path: "/phone/articleDetail",
    query: {
      article_id: utils.encryptCode({ article_id: article_id }),
      path: "index",
    },
  });
};

/**
 * @name: 子组件向父组件抛出的方法
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
const goIndex = (response: any): void => {
  try {
    data.page = response.page;
    data.category_id = response.category_id;
    if (data.page > 1) {
      // 数组合并
      data.dataList.push(...response.articleShow);
    } else {
      // 数组赋值
      data.dataList = response.articleShow;
    }
    data.categoryList = response.cateList;
    data.articlePage = response.articlePage;
  } catch (error) {
    console.log("出错了");
  }
};

/**
 * @name: 获取搜索数据
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
const getSearchData = (): void => {
  data.page = 1;
  MenuRef.value?.getIndexDataList(data.page, data.search, data.category_id);
};

/**
 * @name: 加载下一页数据
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
const nextPage = (): void => {
  data.page += 1;
  // 调用子组件的方法
  MenuRef.value?.getIndexDataList(data.page, data.search, data.category_id);
};

/**
 * @name: 获取分类下的文章列表
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
const onClickTab = (obj: number): void => {
  data.category_id = obj;
  data.page = 1;
  // 调用子组件的方法
  MenuRef.value?.getIndexDataList(data.page, "", data.category_id);
};

/**
 * @name: nextTick 页面发生变化即渲染
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
nextTick(() => {
  /*// 获取子组件name
    console.log(MenuRef._value)
    console.log(MenuRef.__v_isRef)
    console.log(MenuRef.__v_isShallow)
    console.log(MenuRef._rawValue)
    console.log(MenuRef.value.count)
    // 加个问号这种写法，没有属性也不会报错
    console.log(MenuRef.value?.homeColor)
    // 执行子组件方法
    MenuRef.value.goIndex()//*/
});
/**
 * @name: 生命周期---页面挂载完成
 * @author: camellia
 * @email: guanchao_gc@qq.com
 * @date: 2022-07-18
 */
const mounted = onMounted(() => {
  // console.log('获取子组件中的性别', MenuRef );
  // console.log('获取子组件中的其他信息', MenuRef.value?.info );
  // console.log('获取子组件中的其他信息', MenuRef.value.homeColor );
  // 执行子组件方法
  MenuRef.value?.getIndexDataList(data.page);
});
</script>

<style>
em {
  color: #f73131;
}
</style>

<style scoped lang="scss">
@import "/@/assets/css/phone/index.scss";
</style>
